package com.dhsc.service.impl.xtqxgl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dhsc.domain.ywzx.xtqxgl.YhbqxxVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.xtqxgl.IYhbqxxDao;
import com.dhsc.service.xtqxgl.IYhbqxxService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: YhbqxxServiceImpl
 * @Description: 用户标签信息
 * @date: 2023/11/16 13:43
 * @author: gx
 */
@Service
@Slf4j
public class YhbqxxServiceImpl implements IYhbqxxService {
    
    @Autowired
    private IYhbqxxDao yhbqxxDao;
    
    @Override
    public LayuiTableVo queryYhbqxx(YhbqxxVo yhbqxxVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(yhbqxxVo.getPage(), yhbqxxVo.getLimit());
            List<YhbqxxVo> list = yhbqxxDao.queryYhbqxx(yhbqxxVo);
            PageInfo<YhbqxxVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询用户标签信息出错",e);
            throw new MyException("查询用户标签信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveSxdxpz(YhbqxxVo yhbqxxVo) {
        ResponseObject obj = new ResponseObject();
        try {
            //查询是否存在互斥
            List<String> isHc = yhbqxxDao.queryIsHc(yhbqxxVo);
            if (CommonUtil.isNotEmptyList(isHc)){
                obj.setStatus(202);
                obj.setMessage("添加属性中存在互斥规则");
                return obj;
            }else{
                //如果不存在互斥，则添加
                yhbqxxDao.insertSxdxpz(yhbqxxVo);
            }
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("保存属性对象配置出错", e);
            throw new MyException("保存属性对象配置出错",e);
        }
        return obj;
    }

    @Transactional("localTransactionManager")
    @Override
    public ResponseObject deleteSxdxpz(YhbqxxVo yhbqxxVo) {
        ResponseObject obj = new ResponseObject();
        try {
            //状态正常且与权限相关
            yhbqxxDao.updateZcXg(yhbqxxVo);
            //状态正常且与权限无关
            yhbqxxDao.updateZcWg(yhbqxxVo);
            //待审核的标签直接删除
            yhbqxxDao.updateDsh(yhbqxxVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("删除属性对象配置出错", e);
            throw new MyException("删除属性对象配置出错", e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo querySxdxpz(YhbqxxVo yhbqxxVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(yhbqxxVo.getPage(), yhbqxxVo.getLimit());
            List<YhbqxxVo> list = yhbqxxDao.querySxdxpz(yhbqxxVo);
            PageInfo<YhbqxxVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询属性对象配置出错",e);
            throw new MyException("查询属性对象配置出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject querySxxz(YhbqxxVo yhbqxxVo) {
        ResponseObject obj = new ResponseObject();
        try {
            //查询属性选择
            List<YhbqxxVo> list = yhbqxxDao.querySxxz(yhbqxxVo);
            List<String> kslx = list.stream().map(YhbqxxVo::getKslx).collect(Collectors.toList());
            YhbqxxVo yhbqxx = new YhbqxxVo();
            String kslxstr = kslx.stream().collect(Collectors.joining(","));
            yhbqxx.setKslxstr(kslxstr);
            yhbqxx.setBadge(yhbqxxVo.getBadge());
            //查询考试是否合格
            List<String> kslxList = yhbqxxDao.queryIsHg(yhbqxx);
            List<YhbqxxVo> newList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                YhbqxxVo vo = list.get(i);
                int num = 0;
                if (!CommonUtil.isNotEmptyObj(vo.getKslx())){
                    //如果考试类型为空,展示
                    num++;
                }else{
                    for (int j = 0; j < kslxList.size(); j++) {
                        //如果存在合格，则展示
                        if (vo.getKslx().equals(kslxList.get(j))){
                            num++;
                        }
                    }
                }

                if (num > 0){
                    newList.add(vo);
                }
            }
            obj.setData(newList);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("查询属性选择出错", e);
            throw new MyException("查询属性选择出错",e);
        }
        return obj;
    }
}
